<?php
/**
*
*===================================================================
*
*  phpBB Code Repository -- Installer File
*-------------------------------------------------------------------
*	Script info:
* Version:		2.0.0 - "Xerxes" - Alpha 1
* SVN ID:		$Id: install_update.php 3 2009-07-31 02:25:51Z sTraTosphere.programming $
* Copyright:	(c) 2008, 2009 | Obsidian
* Copyright:	(c) 2008, 2009 | Deadpool (original author) -- http://the-webmasters-resource.info
* License:		http://opensource.org/licenses/gpl-2.0.php | GNU Public License v2
* Package:		Install
*
*===================================================================
*
*/

/**
* @ignore
*/
if (!defined('IN_INSTALL'))
{
	exit;
}

if (!isset($config['crs_version']))
{
	// if this value exists it is not already installed... so we don't need this module
	return;
}
elseif(!version_compare($config['crs_version'], CRS_VERSION, '<'))
{
	// if we're not running an old version, we don't need this module either.
	return;
}

if (!empty($setmodules))
{
	$module[] = array(
		'module_type'		=> 'install',
		'module_title'		=> 'UPDATE',
		'module_filename'	=> substr(basename(__FILE__), 0, -strlen(PHP_EXT)-1),
		'module_order'		=> 20,
		'module_subs'		=> '',
		'module_stages'		=> array('MAIN', 'DATABASE', 'FINAL'),
		'module_reqs'		=> ''
	);
}

/**
* Installation
* @package install
*/
class install_update extends installer_core
{
	public function __construct(&$p_master)
	{
		$this->p_master = &$p_master;
	}

	public function main($mode, $sub)
	{
		global $user, $template, $phpbb_root_path, $phpEx;

		switch ($sub)
		{
			case 'main':
				$this->crs_update_main();
				$this->tpl_name = 'install_accept';
			break;

			case 'database':
				$this->crs_db_updater();	
				$this->tpl_name = 'install_body';
			break;

			case 'final':
				$this->page_title = $user->lang['STAGE_FINAL'];
				$this->tpl_name = 'install_body';

				// Version update ONLY AT THE END.
				set_config('crs_version', CRS_VERSION);

				$template->assign_vars(array(
					'TITLE'			=> $user->lang['STAGE_FINAL'],
					'BODY'			=> $user->lang['INSTALL_FINAL_BODY'],
					'L_SUBMIT'		=> $user->lang['VIEW_CRS'],
					'U_ACTION'		=> append_sid(PHPBB_ROOT_PATH . 'code.' . PHP_EXT),
				));
			break;
		}
	}

	private function crs_db_updater()
	{
		global $user, $template, $mode, $config, $db;

		// If this config value exists we /must/ have installed it *hopefully* so no need to run it again...
		switch ($config['crs_version'])
		{
			case '1.0.0 RC 2':
				//Nil.
			case '1.0.0 RC 3':
				//Nil, once again.
			case '1.0.0':
				//No DB changes this go around.
			case '1.0.1':
				// Code Repository update for 1.0.2 ($id$ replacement feature)
				set_config('crs_use_idinfo_replace', 0);
				set_config('crs_idinfo_replacement', 'Code Repository File');
			case '1.0.2':
				//Nil...again...
			case '1.0.3':
				//Nil...
			case '1.0.4':
				$sql = 'DELETE FROM ' . CONFIG_TABLE . "
					WHERE config_value = 'crs_use_idinfo_replace' OR config_value = 'crs_idinfo_replacement'";
				$db->sql_query($sql);
			case '1.0.5':
				// Upgrade from Juno line!
				
				
				// $config['crs_source_path'] isn't new, we're just changing it a bit.
				set_config('crs_source_path', 'projects');		
				
				// These are new config vars.
				set_config('crs_allow_project_notify', 0);
				set_config('crs_packs_path', 'store/packs');
				set_config('crs_livepack_max_age', 2);
				set_config('crs_package_extension', 0);
				set_config('crs_project_index_limit', 7);
				set_config('crs_livepack_skip_cache', 0);
					// Global enable/disable of features/modules.
				set_config('crs_use_code_viewer', 1);
				set_config('crs_use_rss', 1);
				set_config('crs_use_comments', 1);
				set_config('crs_use_download', 1);
				set_config('crs_use_update_subscribe', 1);
				set_config('crs_use_livepack', 1);
				
				// Now let's add some auths...
				if(!class_exists('auth_admin'))
				{
					include(PHPBB_ROOT_PATH . 'includes/acp/auth.' . PHP_EXT);
				}
				$auth_admin = new auth_admin();
				$auth_admin->acl_add_option(array(
					'local'		=> array(),
					'global'	=> array(
						// Admin stuffs
						'a_crs_add_project',
						'a_crs_edit_project',
						'a_crs_project_settings',
				
						// Global permission for Code Repository (Without it, no access at all)
						'u_crs_project_use',
					
						// Main project system stuff
						'u_crs_project_main_list',
						'u_crs_project_main_view',
						'u_crs_project_subscribe',
						
						// Project extensions
						// -- Project History
						'u_crs_project_history_view',
						'u_crs_project_history_download',
						// -- Project Code Repository
						'u_crs_project_coderepo_view',
						'u_crs_project_coderepo_viewfile',
						'u_crs_project_coderepo_viewimage',
						'u_crs_project_coderepo_highlight',
						// -- Project Comments
						'u_crs_project_comments_view',
						'u_crs_project_comments_make',
						'u_crs_project_comments_report',
						'u_crs_project_comments_edit',
						'u_crs_project_comments_delete',
						// These are user-level permissions, but are intended for moderators.
						// We don't want project members getting MCP access just to moderate the comments pages, 
						// since there's not going to be an MCP module for that, you know.  ;)
						'u_crs_project_comments_m_edit',
						'u_crs_project_comments_m_delete',
						'u_crs_project_comments_a_edit',
						'u_crs_project_comments_a_delete',

						// -- Project Download
						'u_crs_project_download',
						// -- Project Live Package Generator
						'u_crs_project_livepack',
					),
				));
				
				// And now we'll remove some auths from 1.0.x "Juno".
				remove_permissions(array(
					'local'		=> array(),
					'global'	=> array(
						'u_crs_view', 
						'u_crs_viewfile', 
						'u_crs_highlightfile', 
						'u_crs_download', 
						'u_crs_viewimage',
					),
				));
			
			// Upgrade fin - 1.0.x "Juno" to 2.0.x "Xerxes".
			
			/*
			case '2.0.0 Alpha 1':
				// This is all for later. Do not uncomment. :P

				// New config vars for the newly implemented features.
				set_config('crs_use_tracker', 1);
				set_config('crs_use_tasks', 1);
				
				// Now let's add some auths...
				if(!class_exists('auth_admin'))
				{
					include(PHPBB_ROOT_PATH . 'includes/acp/auth.' . PHP_EXT);
				}
				$auth_admin = new auth_admin();
				$auth_admin->acl_add_option(array(
					'local'		=> array(),
					'global'	=> array(
						// -- Project Bug Tracker
						//'u_crs_project_tracker_view',
						//'u_crs_project_tracker_make',
						//'u_crs_project_tracker_reply',
						// -- Project Tasks
						//'u_crs_project_tasks_view',
					),
				));
					
			*/
			break;
		}

		$this->page_title = $user->lang['STAGE_DATABASE'];

		$template->assign_vars(array(
			'TITLE'			=> $user->lang['STAGE_DATABASE'],
			'BODY'			=> $user->lang['INSTALL_DATABASE_BODY'],
			'L_SUBMIT'		=> $user->lang['CRS_NEXT_STEP'],
			'U_ACTION'		=> append_sid($this->p_master->module_url . "?mode=$mode&amp;sub=final"),
		));		
	}

	private function crs_update_main()
	{
		global $user, $template, $mode;

		$this->page_title = $user->lang['STAGE_MAIN'];

		if (!request_var('install_crs', 0))
		{
			$template->assign_vars(array(
				'TITLE'			=> $user->lang['STAGE_MAIN'],
				'BODY'			=> $user->lang['INSTALL_INTRO_BODY'],
				'AGREE_INSTALL_CRS'		=> sprintf($user->lang['AGREE_INSTALL_CRS'], CRS_VERSION),

				'L_SUBMIT'		=> $user->lang['CRS_NEXT_STEP'],
				'U_ACTION'		=> append_sid($this->p_master->module_url . "?mode=$mode"),
			));
		}
		else
		{
			redirect(append_sid($this->p_master->module_url . "?mode=$mode&amp;sub=database"));	
		}
	}
}

?>